let Alpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

function DeleteChar(iString, position) {
    let Temp = "";
    let Size = iString.length;
    for (let i = 0; i < Size; i++) {
        if (i != position) {
            Temp = Temp + iString.charAt(i);
        }
    }
    return Temp;
}

function encrypt(input_text, input_keyword) {
    let kw_text = input_text;
    let kw_word = input_keyword.toUpperCase();
    let Beta = "";
    let Gamma = Alpha;
    for (let ix = 0; ix < kw_word.length; ix++) {
        let Let = kw_word.charAt(ix);
        let x = Alpha.indexOf(Let); // check if key character is letter
        if (x == -1) {
            return null
        } else {
            x = Gamma.indexOf(Let);
            if (x != -1) {
                Beta = Beta + Let;
                Gamma = DeleteChar(Gamma, x);
            }
        }
    }
    for (let i = 0; i < Gamma.length; i++) {
        Beta = Beta + Gamma.charAt(i); // finish off rest of alphabet
    }
    kw_text = kw_text.toUpperCase(); // convert input string to upper case
    let Size = kw_text.length; // find out how long input string is
    let CipherText = "";
    let CipherAlpha = "";
    for (let count = 0; count < Size; count++) {
        let alpha = kw_text.charAt(count);
        let idx = Alpha.indexOf(alpha);
        if (idx == -1) {
            CipherAlpha = alpha;
        } else {
            CipherAlpha = Beta.charAt(idx);
        }
        CipherText = CipherText + CipherAlpha;
    }
    return CipherText
}

function decrypt(input_text, input_keyword) {
    let kw_text = input_text;
    let kw_word = input_keyword.toUpperCase();
    let Beta = "";
    let Gamma = Alpha;
    for (let ix = 0; ix < kw_word.length; ix++) {
        let Let = kw_word.charAt(ix);
        let x = Alpha.indexOf(Let); // check if key character is letter
        if (x == -1) {
            return null
        } else {
            x = Gamma.indexOf(Let);
            if (x != -1) {
                Beta = Beta + Let;
                Gamma = DeleteChar(Gamma, x);
            }
        }
    }
    for (let i = 0; i < Gamma.length; i++) {
        Beta = Beta + Gamma.charAt(i); // finish off rest of alphabet
    }
    kw_text = kw_text.toUpperCase(); // convert input string to upper case
    let Size = kw_text.length; // find out how long input string is
    let PlainText = "";
    let CipherAlpha = "";
    for (let count = 0; count < Size; count++) {
        let alpha = kw_text.charAt(count);
        let idx = Beta.indexOf(alpha);
        if (idx == -1) {
            CipherAlpha = alpha;
        } else {
            CipherAlpha = Alpha.charAt(idx);
        }
        PlainText = PlainText + CipherAlpha;
    }
    return PlainText
}

export default {
    encrypt,
    decrypt
}
